package com.lagou.studentsystem.util;

import java.sql.*;

public class DBUtil {

    //驱动信息
    private static String jdbcName;

    //url信息
    private static String dburl;

    //用户名信息
    private static String dbUserName;

    //密码信息
    private static String dbPassWord;

    //进行静态成员的初始化
    static {
        jdbcName = "com.mysql.jdbc.Driver";
        dburl = "jdbc:mysql://localhost:3306/db_javaweb_homework";
        dbUserName = "root";
        dbPassWord = "lxy0611";
        try {
            Class.forName(jdbcName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        Connection con = DriverManager.getConnection(dburl,dbUserName,dbPassWord);
        return con;
    }


    /**
     * 关闭连接
     * @param con
     * @throws SQLException
     */
    public static void closeConnection(Connection con) throws SQLException {
        if (null!= con){
            con.close();
        }
    }

    /**
     * 关闭连接
     * @param con
     * @throws SQLException
     */
    public static void close(Connection con, Statement statement, ResultSet resultSet) throws SQLException {
        if (null!= con && null != statement && null != resultSet){
            con.close();
            statement.close();
            resultSet.close();
        }
    }
}
